import Home   from '@/components/home'
import store  from '@/store'
const _import = require('./_import_' + process.env.NODE_ENV)
import router from '@/router'

/*path: '/home',
	 component: Home,
	 redirect: '/home/detail',
	 children: [ 
	 	{
	 	path: 'detail',
	    component: _import('customerInfo'),
	    name: 'details',
	    meta: { title: '首页详情', icon: 'dashboard', noCache: true }
		}
	  ]
	}*/


var forCycle = function(array,ischild){
	const permissonRouter = [];
	array.forEach(function(v){ 
		const router = {}; 
		router.path = v.path;
    	router.component = Home;
    	router.redirect = v.redirect;
    	router.hidden = v.hidden;
    	if(ischild){
    		router.component = _import(vueModule);
    		router.meta={
    			title: title, icon: icon, noCache: true
    		}
    	}
    	if(router.children){
    		router.children = forCycle(router.children,true);
    	}
	});
	return permissonRouter;
}


export function installRouter(jsonStr){
	//jsonStr是后台权限路由对象数组，现组装路由格式
	const routers =  forCycle(jsonStr,false);
	store.dispatch('GenerateRoutes', { routers }).then(() => { // 根据roles权限生成可访问的路由表
        router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
        next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
    })
}